package com.qys.livesMall.system.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.system.model.dto.sys.H2ChannelDTO;
import com.qys.livesMall.system.model.entity.Country;
import com.qys.livesMall.system.model.entity.H2Channel;
import com.qys.livesMall.system.service.CountryService;
import com.qys.livesMall.system.service.SysH2ChannelService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sys/channel")
public class SysH2ChannelController {

    @Resource
    private SysH2ChannelService sysH2ChannelService;

    @Autowired
    private CountryService countryService;

    @PostMapping("/list")
    @Operation(description = "通道列表")
    public R listChannel(@RequestBody H2Channel channel) {
        if (channel==null){
            channel=new H2Channel();
        }
        LambdaQueryWrapper<H2Channel> query = Wrappers.lambdaQuery(H2Channel.class);
        if (channel.getStatus()!=null) {
            query.eq(H2Channel::getStatus, channel.getStatus());
        }
        query.like(StringUtils.isNotBlank(channel.getChannelName()), H2Channel::getChannelName, channel.getChannelName()) ;
        List<H2Channel> list=sysH2ChannelService.list(query);
        if (CollectionUtils.isEmpty(list)){
            return R.ok(list);
        }
        List<Long> countryIds=list.stream().map(H2Channel::getCountryId).collect(Collectors.toList());
        List<Country>  countries= countryService.listByIds(countryIds);
        Map<Long,Country> countryMap=countries.stream().collect(Collectors.toMap(Country::getId,v->v));
        list.stream().forEach(o->{
            o.setCountry(countryMap.get(o.getCountryId())==null?null:countryMap.get(o.getCountryId()).getChineseName());
        });
        return R.ok(list);

    }

    @PostMapping("/save")
    @Operation(description = "添加通道")
    public R saveChannel(@RequestBody H2ChannelDTO h2ChannelDTO) {
        H2Channel channel = new H2Channel();
        channel.setType(h2ChannelDTO.getType());
        channel.setChannelName(h2ChannelDTO.getChannelName());
        channel.setCountryId(h2ChannelDTO.getCountryId());
        if (h2ChannelDTO.getId() != null) {
            channel.setId(h2ChannelDTO.getId());
            sysH2ChannelService.updateById(channel);
        } else {
            channel.setStatus(0);
            sysH2ChannelService.save(channel);
        }
        return R.ok();

    }


    @GetMapping("/status/{id}")
    @Operation(description = "设置通道状态")
    public R editStatus(@PathVariable("id") Integer id) {
        H2Channel channel = sysH2ChannelService.getById(id);
        int status = channel.getStatus() == 0 ? 1 : 0;
        channel.setStatus(status);
        sysH2ChannelService.updateById(channel);
        return R.ok();
    }

}
